package cn.tedu.controller;

import cn.tedu.pojo.Student;
import lombok.experimental.Accessors;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author :Colin
 * @version 创建时间: 2021/7/15 15:26
 */
@RestController
@RequestMapping("student")
@CrossOrigin

public class StudentController {


    @RequestMapping("add")
    public Student add(Student s) throws ClassNotFoundException, SQLException {
        //参数给student类，框架把里面的值进行了set
        System.out.println(s);
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql:///colinbase?characterEncoding=utf8";
        Connection conn = DriverManager.getConnection(url, "root", "root");
        String sql="insert into stu1 values(?,?,?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,s.getUser());
        ps.setDouble(2,s.getAge());
        ps.setInt(3,s.getSex());
        ps.setString(4,s.getLike());
        ps.setInt(5,s.getEdu());
        ps.setString(6,s.getDate());
        ps.executeUpdate();
        ps.close();
        conn.close();
        System.out.println("success！！！11");
        return s;
    }
}
